﻿<cfcomponent>
	<cfset variables.data = structNew()>
	<cfset variables.json = "">
	<cfset variables.key = "">
	<cfset variables.kind = "PersistentObject">

<!---
	<cfquery datasource="#application.DS#" name="">
		IF NOT EXISTS (
			SELECT	*
			FROM	INFORMATION_SCHEMA.tables
			WHERE	table_name = '#variables.kind#'
		)
		CREATE TABLE #variables.kind# (
			objKey		INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
			objData		NTEXT DEFAULT '' NOT NULL
		)
	</cfquery>
--->

<!---  --->
	<cffunction name="load" returntype="Any">
		<cfargument name="key" default="-77" required="false" type="numeric">

		<cfset var I = "">
		<cfset var result = "">

		<cfquery datasource="#application.DS#" name="result">
			SELECT	*
			FROM	#variables.kind#
			WHERE	objKey = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.key#">
		</cfquery>

		<cfif result.RecordCount>
			<cfset variables.key = result.objKey>
			<cfset variables.json = result.objData>
			<cfset variables.data = deserializejson(variables.json)>
		</cfif>
	</cffunction>
<!---  --->
	<cffunction name="onMissingMethod" returntype="Any">
		<cfargument name="missingMethodName" required="true" type="string">
		<cfargument name="missingMethodArguments" required="false" type="struct">

		<cfset var I = "">

		<!--- GETTER/SETTER METHODOLOGY IS HANDLED HERE --->
		<cfif structcount(arguments.missingMethodArguments) EQ 1>
			<cfset variables.data[arguments.missingMethodName] = duplicate(arguments.missingMethodArguments[1])>
		<cfelseif structcount(arguments.missingMethodArguments) GT 1>
			<cfloop collection="#arguments.missingMethodArguments#" item="I">
				<cfset variables.data[arguments.missingMethodName][I] = arguments.missingMethodArguments[I]>
			</cfloop>
		<cfelse>
			<cfreturn variables.data[arguments.missingMethodName]>
		</cfif>
	</cffunction>
<!---  --->
	<cffunction name="save" returntype="Any">
		<cfset var newRec = "">

		<cfset variables.json = serializejson(variables.data)>

		<cfif isvalid("integer", variables.key)>
			<cfquery datasource="#application.DS#">
	        	UPDATE	#variables.kind#
				SET		objData = '#variables.json#'
				WHERE	objKey = #variables.key#
	        </cfquery>
		<cfelse>
			<cfquery datasource="#application.DS#" result="newRec">
            	INSERT INTO #variables.kind# (
					objData
				) VALUES (
					'#variables.json#'
				)
            </cfquery>
			<cfset variables.key = newRec.identityCol>
		</cfif>
	</cffunction>
<!---  --->
	<cffunction name="search" returntype="Array">
		<cfargument name="searchVariable" default="" required="false" type="any">

		<cfset var I = 0>
		<cfset var result = arrayNew(1)>
		<cfset var Q = "">
		<cfset var searchTerm = serializejson(arguments.searchVariable)>

		<cfquery datasource="#application.DS#" name="Q">
			SELECT	*
			FROM	#variables.kind#
			WHERE	objData LIKE '#searchTerm#'
		</cfquery>

		<cfloop query="Q">
			<cfset result[++I] = createobject("component", variables.kind)>
			<cfset result[I].load(Q.objKey)>
		</cfloop>

		<cfreturn result>
	</cffunction>
</cfcomponent>